'''
rgeMenuBar - for the Random Game Generator project
By Doctus (kirikayuumura.noir@gmail.com)

Menu bar and menu items.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
'''

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import PYQT_VERSION_STR
from rgeSystem import translate, mainWindow, SAVE_DIR, VERSION, DEV
from rgeJson import loadString, jsonload
import sys, os, rgeStyles

ICON_SELECT = 0
ICON_MOVE = 1
ICON_DRAW = 2
ICON_DELETE = 3

class menuBar(object):
    """An object representing the menu bar."""

    def __init__(self):

        main = mainWindow

        self.menubar = main.menuBar()

        # ACTIONS

        self.newModuleAct = QtGui.QAction(translate("menubar", "&New Module..."), main)

        self.loadModuleAct = QtGui.QAction(translate("menubar", "Load Module..."), main)

        self.closeModuleAct = QtGui.QAction(translate("menubar", "Close Module..."), main)

        self.moduleModeAct = QtGui.QAction(translate("menubar","Edit Module Settings"), main)
        self.tilesetModeAct = QtGui.QAction(translate("menubar", "Edit Tilesets"), main)
        self.sceneModeAct = QtGui.QAction(translate("menubar", "Edit Scenes"), main)
        self.spriteModeAct = QtGui.QAction(translate("menubar", "Edit Sprites"), main)
        self.actorModeAct = QtGui.QAction(translate("menubar", "Edit Actors"), main)


        self.gfxSettingsAct = QtGui.QAction(translate("menubar", "Configure Graphics..."), main)

        self.aboutAct = QtGui.QAction(translate("menubar", "&About"), main)
        self.aboutAct.setShortcut("Ctrl+A")



        # MENUS

        fileMenu = QtGui.QMenu(translate("menubar", "&File"), main)
        fileMenu.addAction(self.newModuleAct)
        fileMenu.addAction(self.loadModuleAct)
        fileMenu.addSeparator()
        fileMenu.addAction(self.closeModuleAct)


        self.editingModeMenu = QtGui.QMenu(translate("menubar", "&Mode"), main)
        self.editingModeMenu.addAction(self.moduleModeAct)
        self.editingModeMenu.addAction(self.tilesetModeAct)
        self.editingModeMenu.addAction(self.sceneModeAct)
        self.editingModeMenu.addAction(self.spriteModeAct)
        self.editingModeMenu.addAction(self.actorModeAct)


        stylesMenu = QtGui.QMenu(translate("menubar", "&Styles"), main)
        for style in rgeStyles.sheets.keys():
            stylesMenu.addAction(QtGui.QAction(style, main))

        self.langMenu = QtGui.QMenu(translate("menubar", "&Language"), main)
        ned = QtGui.QAction(translate("menubar", "Dutch"), main)
        ned.setIconText("Dutch")
        self.langMenu.addAction(ned)
        eng = QtGui.QAction(translate("menubar", "English"), main)
        eng.setIconText("English")
        self.langMenu.addAction(eng)
        nhn = QtGui.QAction(translate("menubar", "Japanese"), main)
        nhn.setIconText("Japanese")
        self.langMenu.addAction(nhn)
        deu = QtGui.QAction(translate("menubar", "German"), main)
        deu.setIconText("German")
        self.langMenu.addAction(deu)

        self.optionsMenu = QtGui.QMenu(translate("menubar", "&Options"), main)
        self.optionsMenu.addAction(self.gfxSettingsAct)

        self.pluginsMenu = QtGui.QMenu(translate("menubar", "&Plugins"), main)

        self.pluginsModules = []
        self.plugins = {}
        sys.path.append('plugins')
        for folder in os.listdir('plugins'):
            if folder == ".svn":
                continue
            try:
                self.pluginsModules.append(__import__(folder))
                self.pluginsMenu.addAction(QtGui.QAction(unicode(self.pluginsModules[-1].title()), main))
                self.plugins[unicode(self.pluginsModules[-1].title())] = folder
            except:
                pass

        self.windowMenu = QtGui.QMenu(translate("menubar", "Window"), main)

        self.helpMenu = QtGui.QMenu(translate("menubar", "&Help"), main)
        self.helpMenu.addAction(self.aboutAct)

        # MENUBAR

        self.menubar.addMenu(fileMenu)
        self.menubar.addMenu(self.editingModeMenu)
        self.menubar.addMenu(stylesMenu)
        self.menubar.addMenu(self.optionsMenu)
        self.menubar.addMenu(self.langMenu)
        self.pluginhide = self.menubar.addMenu(self.pluginsMenu)
        if "linux" in sys.platform and list(int(r) for r in PYQT_VERSION_STR.split(".")) < [4,  8,  0]:
            warning = QtGui.QMessageBox()
            warning.setText("Your version of PyQt under Linux is incompatible with RGE's Window menu. Right-click on the menu bar to get an alternate menu.")
            warning.exec_()
        else:
            self.menubar.addMenu(self.windowMenu)
        self.menubar.addMenu(self.helpMenu)

        # EVENTS


        stylesMenu.triggered.connect(self.changeStyle)
        self.pluginsMenu.triggered.connect(self.loadPlugin)

        self.windowMenu.aboutToShow.connect(self.updateWidgetMenu)

        self.aboutAct.triggered.connect(self.about)


    def loadPlugin(self, act):
        exec("from " + (self.plugins[unicode(act.text())]) + " import " + (self.plugins[unicode(act.text())]))
        exec(self.plugins[unicode(act.text())] + ".hajimaru(mainWindow)")
        self.pluginsMenu.removeAction(act)
        if len(self.pluginsMenu.actions()) == 0:
            self.pluginhide.setVisible(False)

    def changeStyle(self, act):
        mainWindow.setStyleSheet(rgeStyles.sheets[unicode(act.text())])

    def updateWidgetMenu(self):
        self.windowMenu.clear()
        for action in mainWindow.createPopupMenu().actions():
            self.windowMenu.addAction(action)

    def about(self):
        msg = QtGui.QMessageBox()
        if DEV:
            aboutText = " ".join(("RGE", VERSION, "Development Version"))
        else:
            aboutText = " ".join(("RGE", VERSION, "Release"))
        msg.setText(aboutText)
        msg.setInformativeText("http://code.google.com/p/random-game-editor/")
        msg.setWindowTitle("About")
        msg.exec_()
